package com.szpx.utils.jsonutils;

/**
 *  json处理工具类，统一返回格式
 */
public class ApiResult {
    //服务端返回的状态码
    private int code = 200;
    //服务端返回状态信息
    private String msg = "success";
    //服务端返回的数据
    private Object data;
    //数据总数
    private long total;

    public ApiResult(int code, String msg,Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public ApiResult(int code, String msg,Object data, long total) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.total= total;
    }

    /**
     * 只返回一条数据
     * @param data
     * @return
     * @param <Object>
     */
    public static <Object> ApiResult putOneData(Object data){
        ApiResult resp = new ApiResult(200,"success",data,1);
        return resp;
    }


    /**
     * 返回列表数据
     * @param data
     * @param total
     * @return
     * @param <Object>
     */
    public static <Object> ApiResult putListData(Object data,long total){
        ApiResult resp = new ApiResult(200,"success",data,total);
        return resp;
    }

    public static <Object> ApiResult success(Object data){
        ApiResult resp = new ApiResult(200, "success", data);
        return resp;
    }

    public static <Object> ApiResult error(int code,String msg){
        ApiResult resp = new ApiResult(code,msg, null);
        return resp;
    }

    public int getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public Object getData() {
        return data;
    }

    public Object getTotal() {
        return total;
    }
}
